package com.java复习.集合的学习;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Mapmain {
    static Map bookmap = new HashMap();
    static Set set = bookmap.keySet();

    public static void main() {
        while (true) {
            Scanner myScanner = new Scanner(System.in);
            System.out.println("=====图书管理系统=====");
            System.out.println("1.添加书籍（书号，书名）");
            System.out.println("2.查询全部数据");
            System.out.println("3.根据书号查书名");
            System.out.println("4.根据书号删除书籍");
            System.out.println("5.根据书号修改书名");
            System.out.println("6.退出系统");
            System.out.println("请输入你要选择的功能前的序号：");
            while(!myScanner.hasNextInt())
            {
                //没有这一行的话会导致无限循环
                String useless = myScanner.next();
                System.out.println("不是数字，重新输入");
            }
            //当是数字时循环结束
            int a = myScanner.nextInt();

            if (a > 0 && a < 6) {
                a = a;
                switch (a) {
                    case 1:
                        Mapadd();
                        System.out.println(bookmap);
                        break;
                    case 2:
                        showall();
                        break;
                    case 3:
                        find();
                        break;
                    case 4:
                        remove();
                        break;
                    case 5:
                        change();
                        break;

                }
            } else {
                System.out.println("===========退出系统===========");
                break;
            }

        }


    }

    public static void Mapadd() {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("输入要添加的书号：");
        int id = myScanner.nextInt();
        System.out.println("输入要添加的书名：");
        String name = myScanner.next();
        bookmap.put(id, name);
        System.out.println("=====添加成功=====");
    }

    public static void showall() {

        for (Object key : set) {
            System.out.println(key + "=====" + bookmap.get(key));
        }
    }

    //根据书号查书名
    public static void find() {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("请输入要查找书籍的书号：");
        int iid = myScanner.nextInt();
        for (Object key : set) {
            if (iid == ((Book) set).getId()) {
                System.out.println("书籍号" + ((Book) set).getId() + "对应的" + "书籍名称为：" + ((Book) set).getName());
                break;
            } else if (iid == 0) {
                System.out.println("请重新输入");
                break;
            }
        }
    }

    // 4.根据书号删除书籍

    public static void remove() {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("请输入要删除书籍的书号：");
        int iid = myScanner.nextInt();
        for (Object key : set) {
            System.out.println(key+"====="+bookmap.get(key));
            bookmap.remove(iid);
            System.out.println("=====删除成功=====");
            break;
        }

    }
//
//
    // 5.根据书号修改书名
    public static void change(){
        Scanner myScanner = new Scanner(System.in);
        System.out.println("请输入要修改书籍的书号：");
        int iid = myScanner.nextInt();
        System.out.println("请输入你要修改的书名：");
        String name = myScanner.next();
        bookmap.put(iid,name);
    }

}


